package org.vy.dal.dao.lookup;

/**
 * @author Ravi Shankar
 *
 */

import java.util.Date;
import java.util.List;

import org.vy.dal.QueryManager;

public class VyLookupPatrikaDAO {
	private static final String FIND_BY_PK = "SELECT * FROM <TABLE> WHERE PATRIKA_ID = m_PatrikaId";
	private static final String FIND_BY_PARAM = "SELECT * FROM <TABLE> WHERE 1 = 1 ";
	private static final String FIND_ALL_ACTIVE = "SELECT * FROM <TABLE> WHERE 1 = 1 and EFFECTIVE_DATE <= m_EffectiveDate AND IS_DELETED = FALSE";
	private static final String UPDATE = "UPDATE <TABLE> SET PATRIKA_NAME = m_PatrikaName, PER_COPY_AMOUNT = m_PerCopyAmount,EFFECTIVE_DATE=m_EffectiveDate, LAST_UPDATED_BY = m_LastUpdatedBy, LAST_UPDATED_ON = m_LastUpdatedOn WHERE PATRIKA_ID = m_PatrikaId";
	private static final String INSERT = "INSERT INTO <TABLE> (PATRIKA_ID, PATRIKA_NAME, EFFECTIVE_DATE, WEEK_OFFSET_INDEX, IS_DELETED, LAST_UPDATED_BY, LAST_UPDATED_ON) VALUES (m_PatrikaId, m_PatrikaName, m_EffectiveDate, m_WeekOffsetIndex, m_IsDeleted, m_LastUpdatedBy, m_LastUpdatedOn)";
	private static final String DELETE = "UPDATE <TABLE> SET IS_DELETED = TRUE WHERE PATRIKA_ID = m_PatrikaId";

	public static VyLookupPatrikaDO findByPK(Integer key) throws Exception {
		VyLookupPatrikaDO dto = new VyLookupPatrikaDO();
		dto.setPatrikaId(key);
		dto = (VyLookupPatrikaDO) QueryManager.executeQuery(dto, FIND_BY_PK);
		return dto;
	}

	@SuppressWarnings("unchecked")
	public static List<VyLookupPatrikaDO> findAllActive() throws Exception {
		VyLookupPatrikaDO dto = new VyLookupPatrikaDO();
		dto.setEffectiveDate(new Date());
		return (List<VyLookupPatrikaDO>) QueryManager.executeListQuery(dto,
				FIND_ALL_ACTIVE);
	}

	public static VyLookupPatrikaDO insert(VyLookupPatrikaDO dto)
			throws Exception {
		dto = (VyLookupPatrikaDO) QueryManager.executeInsert(dto, INSERT);
		return dto;
	}

	public static VyLookupPatrikaDO update(VyLookupPatrikaDO dto)
			throws Exception {
		int i = QueryManager.executeUpdate(dto, UPDATE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	public static VyLookupPatrikaDO delete(VyLookupPatrikaDO dto) throws Exception {
		int i = QueryManager.executeUpdate(dto, DELETE);
		if (i > 0)
			return dto;
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public static List<VyLookupPatrikaDO> findByParam(VyLookupPatrikaDO dto)
			throws Exception {
		StringBuilder sb = new StringBuilder(FIND_BY_PARAM);
		if (dto.getPatrikaId() != null) {
			sb.append(" AND PATRIKA_ID LIKE '%" + dto.getPatrikaId() + "%' ");
		}
		if (dto.getPatrikaName() != null) {
			sb.append(" AND PATRIKA_NAME LIKE '%" + dto.getPatrikaName()
					+ "%' ");
		}
		if (dto.getEffectiveDate() != null) {
			sb.append(" AND EFFECTIVE_DATE LIKE '%" + dto.getEffectiveDate()
					+ "%' ");
		}
		if (dto.getWeekOffsetIndex() != null) {
			sb.append(" AND WEEK_OFFSET_INDEX LIKE '%"
					+ dto.getWeekOffsetIndex() + "%' ");
		}
		if (dto.getIsDeleted() != null) {
			sb.append(" AND IS_DELETED LIKE '%" + dto.getIsDeleted() + "%' ");
		}
		if (dto.getLastUpdatedBy() != null) {
			sb.append(" AND LAST_UPDATED_BY LIKE '%" + dto.getLastUpdatedBy()
					+ "%' ");
		}
		if (dto.getLastUpdatedOn() != null) {
			sb.append(" AND LAST_UPDATED_ON LIKE '%" + dto.getLastUpdatedOn()
					+ "%' ");
		}
		System.out.println(sb.toString());
		return (List<VyLookupPatrikaDO>) QueryManager.executeListQuery(dto, sb
				.toString());
	}

}
